package com.project.nimsen.weixin;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.UUID;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import com.project.nimsen.util.NimsenUtil;

public class WXMenuMain {
	/**
	 * email:2306476431@qq.com
	 * password:weimeng888
	 * 
	 * appid:wxe56d471560e67557
	 * appsecret:00a98732e04d550670d76f2b16618c8e
	 * 
	 * 认证的：
	 * username:jywxlm
	 * password:jyznsjw91
	 * 
	 * appid:wxfe02c430e04f9ce2
	 * appsecret:0b72727127095e11d24a0fff482f25e2
	 *
	 * 
	 * 
	 * 我的测试账号：
	 * appid:wx322e0f75311e146c
	 * appsecret:db3530674be2326f0dfdcb587f4bf896
	 * 测试openid:oy_wSuJnxJZq-qoMlygg5LxzInQg
	 * 10:00-12:00
	 * */
	private static String appid = "wx322e0f75311e146c";
	private static String appsecret = "db3530674be2326f0dfdcb587f4bf896";
	private static String access_token = "ANfiBvb5FfZH_SgFnk5Mn18VA7Dbg0O6aiRyaq7MC2VOQmgivNF9LOFEm19XdcaVmlPscgf_KvogIb2lzksShQ";
	private static String openid = "oy_wSuJnxJZq-qoMlygg5LxzInQg";
	
	
	public enum bean{blue, red}
	
	public static void main(String [] arg)throws Exception{
		HttpPost httpost = new HttpPost("http://172.16.16.203:8080/nywlw/bingtest/reqDemo");
		JSONObject all = new JSONObject();
		all.put("openid", openid);
		all.put("to_groupid", "100");
		StringEntity string = new StringEntity(all.toString(), ContentType.APPLICATION_JSON);
		httpost.setEntity(string);
		//处理请求
		HttpClient httpclient = new DefaultHttpClient();
		HttpResponse response = httpclient.execute(httpost);
		InputStream in = response.getEntity().getContent();
		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		StringBuffer buffer = new StringBuffer();
		//临时信息
		String info = null;
		while((info=reader.readLine())!=null){
			buffer.append(info);
		}
		System.out.println(buffer.toString());
	}
	//移动用户分组
	private static void move_use_group()throws Exception{
		HttpPost httpost = new HttpPost("https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token="+access_token);
		JSONObject all = new JSONObject();
		all.put("openid", openid);
		all.put("to_groupid", "100");
		StringEntity string = new StringEntity(all.toString(), ContentType.APPLICATION_JSON);
		httpost.setEntity(string);
		//处理请求
		HttpClient httpclient = new DefaultHttpClient();
		HttpResponse response = httpclient.execute(httpost);
		InputStream in = response.getEntity().getContent();
		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		StringBuffer buffer = new StringBuffer();
		//临时信息
		String info = null;
		while((info=reader.readLine())!=null){
			buffer.append(info);
		}
		System.out.println(buffer.toString());
		//{"errcode":0,"errmsg":"ok"}
	}
	//修改分组名称
	private static void update_group_name()throws Exception{
		HttpPost httpost = new HttpPost("https://api.weixin.qq.com/cgi-bin/groups/update?access_token="+access_token);
		JSONObject all = new JSONObject();
		JSONObject group = new JSONObject();
		group.put("id", "100");
		group.put("name", "测试组名修改");
		all.put("group", group);
		StringEntity string = new StringEntity(all.toString(), ContentType.APPLICATION_JSON);
		httpost.setEntity(string);
		//处理请求
		HttpClient httpclient = new DefaultHttpClient();
		HttpResponse response = httpclient.execute(httpost);
		InputStream in = response.getEntity().getContent();
		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		StringBuffer buffer = new StringBuffer();
		//临时信息
		String info = null;
		while((info=reader.readLine())!=null){
			buffer.append(info);
		}
		System.out.println(buffer.toString());
		//{"errcode":0,"errmsg":"ok"}
	}
	//查询用户所在分组
	private static void find_use_group()throws Exception{
		HttpPost httpost = new HttpPost("https://api.weixin.qq.com/cgi-bin/groups/getid?access_token="+access_token);
		JSONObject all = new JSONObject();
		all.put("openid", openid);
		StringEntity string = new StringEntity(all.toString(), ContentType.APPLICATION_JSON);
		httpost.setEntity(string);
		//处理请求
		HttpClient httpclient = new DefaultHttpClient();
		HttpResponse response = httpclient.execute(httpost);
		InputStream in = response.getEntity().getContent();
		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		StringBuffer buffer = new StringBuffer();
		//临时信息
		String info = null;
		while((info=reader.readLine())!=null){
			buffer.append(info);
		}
		System.out.println(buffer.toString());
		//{"groupid":0}
	}
	//添加分组
	private static void group_add()throws Exception{
		HttpPost httpost = new HttpPost("https://api.weixin.qq.com/cgi-bin/groups/create?access_token="+access_token);
		JSONObject all = new JSONObject();
		JSONObject name = new JSONObject();
		name.put("name", "测试分组1");
		all.put("group", name);
		StringEntity string = new StringEntity(all.toString(), ContentType.APPLICATION_JSON);
		httpost.setEntity(string);
		//处理请求
		HttpClient httpclient = new DefaultHttpClient();
		HttpResponse response = httpclient.execute(httpost);
		InputStream in = response.getEntity().getContent();
		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		StringBuffer buffer = new StringBuffer();
		//临时信息
		String info = null;
		while((info=reader.readLine())!=null){
			buffer.append(info);
		}
		System.out.println(buffer.toString());
		//{"group":{"id":100,"name":"测试分组1"}}
	}
	//查询分组信息
	private static void group_search()throws Exception{
		HttpGet httpget = new HttpGet("https://api.weixin.qq.com/cgi-bin/groups/get?access_token="+access_token);
		HttpClient httpclient = new DefaultHttpClient();
		HttpResponse response = httpclient.execute(httpget);
		InputStream in = response.getEntity().getContent();
		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		StringBuffer buffer = new StringBuffer();
		//临时信息
		String info = null;
		while((info=reader.readLine())!=null){
			buffer.append(info);
		}
		JSONObject json = JSONObject.fromObject(buffer.toString());
		System.out.println(buffer.toString());
		//{"groups":[{"id":0,"name":"未分组","count":1},{"id":1,"name":"黑名单","count":0},{"id":2,"name":"星标组","count":0},{"id":100,"name":"测试分组1","count":0}]}
	}
	//得到用户信息
	private static void use_info()throws Exception{
		HttpGet httpget = new HttpGet("https://api.weixin.qq.com/cgi-bin/user/info?access_token="+access_token+"&openid="+openid+"&lang=zh_CN");
		//处理请求
		HttpClient httpclient = new DefaultHttpClient();
		HttpResponse response = httpclient.execute(httpget);
		InputStream in = response.getEntity().getContent();
		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		StringBuffer buffer = new StringBuffer();
		//临时信息
		String info = null;
		while((info=reader.readLine())!=null){
			buffer.append(info);
		}
		JSONObject json = JSONObject.fromObject(buffer.toString());
		System.out.println(buffer.toString());
		/*{
		    "subscribe": 1, 
		    "openid": "o6_bmjrPTlm6_2sgVt7hMZOPfL2M", 
		    "nickname": "Band", 
		    "sex": 1, 
		    "language": "zh_CN", 
		    "city": "广州", 
		    "province": "广东", 
		    "country": "中国", 
		    "headimgurl":    "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/0", 
		   "subscribe_time": 1382694957,
		   "unionid": " o6_bmasdasdsad6_2sgVt7hMZOPfL"
		}*/
		//{"subscribe":1,"openid":"oy_wSuJnxJZq-qoMlygg5LxzInQg","nickname":"半分笑","sex":1,"language":"zh_CN","city":"济源","province":"河南","country":"中国","headimgurl":"http:\/\/wx.qlogo.cn\/mmopen\/ajNVdqHZLLAdCpwabEhpiclpGMmXufzoibz4XoORuAhqAwbbYKWNiar3Xqzl7O69leMg251FpqCqlP1l8t2hMPcsw\/0","subscribe_time":1405843081,"remark":""}
	}
	//得到ticket和于获得永久性二维码
	private static String ticket()throws Exception{
		HttpPost httpost = new HttpPost("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token="+access_token);

		JSONObject all = new JSONObject();
		all.put("action_name", "QR_LIMIT_SCENE");//action_info
		JSONObject action_info = new JSONObject();
		JSONObject scene = new JSONObject();
		scene.put("scene_id", "125");
		action_info.put("scene", scene);
		
		all.put("action_info", action_info);
		
		StringEntity string = new StringEntity(all.toString(), ContentType.APPLICATION_JSON);
		httpost.setEntity(string);
		
		
		//处理请求
		HttpClient httpclient = new DefaultHttpClient();
		HttpResponse response = httpclient.execute(httpost);
		InputStream in = response.getEntity().getContent();
		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		StringBuffer buffer = new StringBuffer();
		//临时信息
		String info = null;
		while((info=reader.readLine())!=null){
			buffer.append(info);
		}
		JSONObject json = JSONObject.fromObject(buffer.toString());
		System.out.println(buffer.toString());
		if(StringUtils.isNotBlank(NimsenUtil.obj2String(json.get("errcode")))){
			System.out.println("出错");
			return null;
		}
		//正确时反回：{"access_token":"ACCESS_TOKEN","expires_in":7200}
		//错误时返回：{"errcode":40013,"errmsg":"invalid appid"}
		return json.getString("ticket");
	}
	//向用户主动发送文本消息
	private static void send_txt_message()throws Exception{
		HttpPost httpost = new HttpPost("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+access_token);
		//文本内容消息
		JSONObject message = new JSONObject();
		message.put("touser", openid);
		message.put("msgtype", "text");
		//文本内容
		JSONObject text = new JSONObject();
		text.put("content", "此消息是杜冰于2014年5月27日的测试消息。");
		message.put("text", text);
		
		StringEntity string = new StringEntity(message.toString(), ContentType.APPLICATION_JSON);
		httpost.setEntity(string);
		
		//处理请求
		HttpClient httpclient = new DefaultHttpClient();
		HttpResponse response = httpclient.execute(httpost);
		InputStream in = response.getEntity().getContent();
		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		StringBuffer buffer = new StringBuffer();
		//临时信息
		String info = null;
		while((info=reader.readLine())!=null){
			buffer.append(info);
		}
		System.out.println(buffer.toString());
	}
	//得到用户列表,需要OAuth2.0网页授权
	private static void user_search()throws Exception{
		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet("https://api.weixin.qq.com/cgi-bin/user/get?access_token="+access_token+"&next_openid=");
		HttpResponse response = httpclient.execute(httpget);
		InputStream in = response.getEntity().getContent();
		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		//临时信息
		StringBuffer buffer = new StringBuffer();
		String info = null;
		while((info=reader.readLine())!=null){
			buffer.append(info);
		}
		//检测是否正确
		JSONObject json = JSONObject.fromObject(buffer.toString());
		System.out.println(buffer.toString());
		if(StringUtils.isNotBlank(NimsenUtil.obj2String(json.get("errcode")))){
			System.out.println("出错！");
		}
		//{"total":1,"count":1,"data":{"openid":["oy_wSuJnxJZq-qoMlygg5LxzInQg"]},"next_openid":"oy_wSuJnxJZq-qoMlygg5LxzInQg"}
	}
	//删除菜单
	private static void menu_delete()throws Exception{
		HttpGet httpget = new HttpGet("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token="+access_token);
		//处理请求
		HttpClient httpclient = new DefaultHttpClient();
		HttpResponse response = httpclient.execute(httpget);
		InputStream in = response.getEntity().getContent();
		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		StringBuffer buffer = new StringBuffer();
		//临时信息
		String info = null;
		while((info=reader.readLine())!=null){
			buffer.append(info);
		}
		//删除成功后返回：{"errcode":0,"errmsg":"ok"}
		System.out.println(buffer.toString());
		
	}
	//查询菜单信息
	private static void menu_search()throws Exception{
		HttpGet httpget = new HttpGet("https://api.weixin.qq.com/cgi-bin/menu/get?access_token="+access_token);
		//处理请求
		HttpClient httpclient = new DefaultHttpClient();
		HttpResponse response = httpclient.execute(httpget);
		InputStream in = response.getEntity().getContent();
		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		StringBuffer buffer = new StringBuffer();
		//临时信息
		String info = null;
		while((info=reader.readLine())!=null){
			buffer.append(info);
		}
		System.out.println(buffer.toString());
	}
	//添加菜单例子
	private static void menu_add()throws Exception{
		HttpPost httpost = new HttpPost("https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+access_token);
		
		//一级菜单集合
		JSONArray firstmenu = new JSONArray();
		//今日哥曲
		JSONObject men_jrgq = new JSONObject();
		men_jrgq.put("type", "view");
		men_jrgq.put("name", "二维码名片制作");
		men_jrgq.put("url", "http://www.nimsen.com/pub/tools/qrcode/card");
		firstmenu.add(men_jrgq);
		//歌手简介
		JSONObject men_gsjj = new JSONObject();
		men_gsjj.put("type", "view");
		men_gsjj.put("name", "歌手简介");
		men_gsjj.put("url", "http://www.google.com.hk");
		firstmenu.add(men_gsjj);
		//演唱会
		JSONObject men_ych = new JSONObject();
		men_ych.put("name", "演唱会");
		//演唱会菜单
		JSONArray ychs = new JSONArray();
		//成都演唱会
		JSONObject men_cd = new JSONObject();
		men_cd.put("type", "view");
		men_cd.put("name", "成都演唱会");
		men_cd.put("url", "http://www.163.com");
		ychs.add(men_cd);
		//北京演唱会
		JSONObject men_bj = new JSONObject();
		men_bj.put("type", "view");
		men_bj.put("name", "北京演唱会");
		men_bj.put("url", "http://www.sina.com");
		ychs.add(men_bj);
		//上海演唱会
		JSONObject men_sh = new JSONObject();
		men_sh.put("type", "view");
		men_sh.put("name", "上海演唱会");
		men_sh.put("url", "http://www.sohu.com");
		ychs.add(men_sh);
		//深圳演唱会
		JSONObject men_sz = new JSONObject();
		men_sz.put("type", "view");
		men_sz.put("name", "深圳演唱会");
		men_sz.put("url", "http://www.taobao.com");
		ychs.add(men_sz);
		//广州演唱会
		JSONObject men_gz = new JSONObject();
		men_gz.put("type", "view");
		men_gz.put("name", "广州演唱会");
		men_gz.put("url", "http://www.qq.com");
		ychs.add(men_gz);
		men_ych.put("sub_button", ychs);
		firstmenu.add(men_ych);
		//菜单接口的根对象
		JSONObject all = new JSONObject();
		all.put("button", firstmenu);
		
		StringEntity string = new StringEntity(all.toString(), ContentType.APPLICATION_JSON);
		httpost.setEntity(string);
		
		//处理请求
		HttpClient httpclient = new DefaultHttpClient();
		HttpResponse response = httpclient.execute(httpost);
		InputStream in = response.getEntity().getContent();
		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		StringBuffer buffer = new StringBuffer();
		//临时信息
		String info = null;
		while((info=reader.readLine())!=null){
			buffer.append(info);
		}
		//正确时返回：{"errcode":0,"errmsg":"ok"}
		//错误时返回：{"errcode":40018,"errmsg":"invalid button name size"}
		System.out.println(buffer.toString());
	}
	//获得access_token
	/**
	 * {"access_token":"ANfiBvb5FfZH_SgFnk5Mn18VA7Dbg0O6aiRyaq7MC2VOQmgivNF9LOFEm19XdcaVmlPscgf_KvogIb2lzksShQ","expires_in":7200}
	 * {"errcode":40013,"errmsg":"invalid appid"}
	 * */
	private static String access_token()throws Exception{
		StringBuffer buffer = new StringBuffer();
		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+appsecret);
		try {
			HttpResponse response = httpclient.execute(httpget);
			InputStream in = response.getEntity().getContent();
			BufferedReader reader = new BufferedReader(new InputStreamReader(in));
			//临时信息
			String info = null;
			while((info=reader.readLine())!=null){
				buffer.append(info);
			}
			//检测是否正确
			JSONObject json = JSONObject.fromObject(buffer.toString());
			System.out.println(buffer.toString());
			if(StringUtils.isNotBlank(NimsenUtil.obj2String(json.get("errcode")))){
				return null;
			}
			//正确时反回：{"access_token":"ACCESS_TOKEN","expires_in":7200}
			//错误时返回：{"errcode":40013,"errmsg":"invalid appid"}
			return json.getString("access_token");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return buffer.toString();
	}
}
